<?php

namespace core\process;

use core\HttpSession;
use core\gateway\AbstractGateway;
use core\execute\method\MethodExecute;
use core\execute\method\Method;
use core\Object;
class ProcessProperty implements Object{

	private static $ATTR_SET  = "set";
	private static $ATTR_NAME = "name";

	private $httpSession;
	private $gateway;
	private $fileHtml;

	public function __construct(HttpSession $httpSession, AbstractGateway $gateway, $fileHtml){
		$this->httpSession = $httpSession;
		$this->gateway = $gateway;
		$this->fileHtml = $fileHtml;
		$this->createProperty();
	}

	private function createProperty(){
		$this->document = new \DOMDocument(null, null);
		$this->document->loadHTMLFile($this->fileHtml);
		$this->processDOMNode($this->document);
	}

	private function processDOMNode(\DOMNode $node){
		if($node instanceof \DOMElement){
			if($node->hasAttribute(static::$ATTR_SET)){
				if($node->hasAttribute(static::$ATTR_NAME)){
					if($this->httpSession->hasParameter($node->getAttribute(static::$ATTR_NAME))){
						$this->setValueProperty($this->gateway, $node->getAttribute(static::$ATTR_SET), $this->httpSession->getParameter($node->getAttribute(static::$ATTR_NAME)));
					}
				}
			}
		}

		if($node->hasChildNodes()){
			foreach ($node->childNodes as $newNode){
				$this->processDOMNode($newNode);
			}
		}
	}

	private function setValueProperty(Object $gateway, $methodParam, $value){
		$methods = explode("->", $methodParam);
		foreach ($methods as $key => $name){
			$parameters = ($key == (count($methods) - 1)) ? array($value) : array();
			$gateway = MethodExecute::execute(Method::createMethod($gateway, $name, $parameters, true));
		}
	}
}
?>